package com.opensky.qatest.ui.page;

import junit.framework.Assert;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.LoadableComponent;

import com.opensky.qatest.ui.component.CuratorsProfilePageJoinModal;
import com.opensky.qatest.ui.component.CuratorsProfilePageJoinNag;
import com.opensky.qatest.util.common.AssertReport;
import com.opensky.qatest.util.common.LogicUtils;
import com.opensky.qatest.util.selenium.HTML;
import com.opensky.qatest.util.selenium.WebDriverWrapper;

/*
 * OpenSky - "Curator's profile page" model
 *
 * @author: Aliakse_Khursevich (hursevich@gmail.com)
 * @version: 1.0
 * 
 */
public class CuratorsProfilePage extends LoadableComponent<CuratorsProfilePage> implements IPage {

    // ************************ Locators ************************

    private static final String LOGOUT_URL = "https://opensky.com/logout";
    
    private static final String JOIN_MODAL = "//div[@id='ui-tooltip-modal' and contains(@style, 'display: block')]//div[@id='join-modal']";
    
    private static final String JOIN_NAG = "//form[contains(@action, '/join?osky_origin')]";

    private static final String HELP_LINK = "//a[@href='/content/faq' and text()='Help']";

    private static final String LOGIN_LINK = "//div[@class='login']/a[text()='Log in']";

//  private static final String CART_LINK = "//div[@id='cart-nav']//span[@class='cart']";

    private static final String TOP_NAV = "//ul[@id='category-nav']";

    private static final String BOTTOM_NAV = "//div[@class='wrap']/ul[@class='links']";

    private static final String FOLLOW_BTN = "//a[@class='button anon-follow' and text()='Follow']";

    private static final String FIRST_GOOD_LINK = "//ul[contains(@class, 'collection-items-grid')]/li[1]//a";

    private static final String PRICE = "//ul[contains(@class, 'collection-items-grid')]/li[1]//div[@class='price' and contains(., '$')]";
    
    private static final String FOLLOWING_MESSAGE = "//form[@class='following']/p[contains(., \"You're following\")]";
    
    private static final String UNFOLLOW_BTN = "//form[@class='following']/input[@value='Unfollow']";


    private String PROFILE_PAGE_URL = "https://opensky.com/%s";

    // ************************ Complex page objects **************************

    private CuratorsProfilePageJoinModal joinModal;

    private CuratorsProfilePageJoinNag joinNag;

    private final WebDriverWrapper driver;

    public CuratorsProfilePage(WebDriverWrapper driver, String curator) {
	this.driver = driver;
	joinModal = new CuratorsProfilePageJoinModal(driver);
	joinNag = new CuratorsProfilePageJoinNag(driver);
	PROFILE_PAGE_URL = String.format(PROFILE_PAGE_URL, curator);
    }

    // ************************ Actions **************************

    @Override
    protected void load() {
	driver.get(PROFILE_PAGE_URL);
    }

    public void clickFollow() {
	driver.waitAndClick("FOLLOW_BTN", By.xpath(FOLLOW_BTN));
    }

    public OfferPage clickOfferLink() {

	WebElement productLink = driver.waitAndGetElement("FIRST_GOOD_LINK", By.xpath(FIRST_GOOD_LINK));

	String href = productLink.getAttribute(HTML.HREF);
	String curator = href.split("/")[3];
	String product = href.split("/")[5];

	productLink.click();
	OfferPage productPage = new OfferPage(driver, curator, product);
	try {
	    productPage.isLoaded();
	} catch (Error e) {
	    return null;
	}

	return productPage;
    }
    
    public LoginPage logOut() {
	
	LoginPage loginPage = new LoginPage(driver);
	
	driver.get(LOGOUT_URL);
	
	if(loginPage.isPageLoaded()) {
	    return loginPage;
	} else {
	    return null;
	}
    }

    // ************************ Validations **************************

    @Override
    protected void isLoaded() throws Error {
	String url = driver.getCurrentUrl();
	Assert.assertTrue(AssertReport.wrongURL(PROFILE_PAGE_URL, url), LogicUtils.isURLEqual(PROFILE_PAGE_URL, url));
    }

    public boolean isPageLoaded() {
	return driver.waitAndVerifyURL(PROFILE_PAGE_URL);
    }


    public boolean isBaseNavElementsPresent() {
	driver.waitAndCheckElementPresent("LOGIN_LINK", By.xpath(LOGIN_LINK));
	driver.waitAndCheckElementPresent("HELP_LINK", By.xpath(HELP_LINK));
//	driver.waitAndCheckElementPresent("CART_LINK", By.xpath(CART_LINK));
	driver.waitAndCheckElementPresent("TOP_NAV", By.xpath(TOP_NAV));
	driver.waitAndCheckElementPresent("BOTTOM_NAV", By.xpath(BOTTOM_NAV));
	return true;
    }

    public boolean isPriceNotPresent() {
	driver.waitAndCheckElementNotPresent("PRICE", By.xpath(PRICE));
	return true;
    }
    
    public boolean isPricePresent() {
	driver.waitAndCheckElementPresent("PRICE", By.xpath(PRICE));
	return true;
    }

    public boolean isFollowBtnPresent() {
	driver.waitAndCheckElementPresent("FOLLOW_BTN", By.xpath(FOLLOW_BTN));
	return true;
    }
    
    public boolean isFollowBtnNotPresent() {
	driver.waitAndCheckElementNotPresent("FOLLOW_BTN", By.xpath(FOLLOW_BTN));
	return true;
    }
    
    public boolean isAfterFollowBaseElementsPResent() {
	driver.waitAndCheckElementPresent("FOLLOWING_MESSAGE", By.xpath(FOLLOWING_MESSAGE));
	driver.waitAndCheckElementPresent("UNFOLLOW_BTN", By.xpath(UNFOLLOW_BTN));
	return true;
    }

    // ************************ GET/SET methods **************************

    public CuratorsProfilePageJoinModal getJoinModal() {
	
	if(driver.waitAndVerifyElementPresent(By.xpath(JOIN_MODAL))) {
	    return joinModal;
	} 
	else {
	    return null;
	}
    }

    public CuratorsProfilePageJoinNag getJoinNag() {
	
	if(driver.waitAndVerifyElementPresent(By.xpath(JOIN_NAG))) {
	    return joinNag;
	} 
	else {
	    return null;
	}
    }

    public String getURL() {
	return PROFILE_PAGE_URL;
    }
}
